package com.kehao.leetcode.basic.design;

import org.junit.Test;

import java.util.Stack;

public class MinStack {
    private Stack<Integer> stack;
    private Stack<Integer> min;

    public MinStack() {
        stack = new Stack<>();
        min = new Stack<>();
    }

    public void push(int val) {
        stack.push(val);
        if(min.isEmpty()){
            min.push(val);
        }else {
            if(val<=min.peek()){
                min.push(val);
            }
        }
    }

    public void pop() {
        if(stack.isEmpty()){
            throw new IllegalStateException("stack is empty");
        }else {
            int pop = stack.pop();
            if(pop==min.peek()){
                min.pop();
            }
        }
    }

    public int top() {
        if(stack.isEmpty()){
            throw new IllegalStateException("is empty");
        }else {
            return stack.peek();
        }
    }

    public int getMin() {
        if(min.isEmpty()){
            throw new IllegalStateException("is empty");
        }else {
            return min.peek();
        }
    }

    @Test
    public void test01(){
        MinStack minStack = new MinStack();
        minStack.push(0);
        minStack.push(1);
        minStack.push(0);
        minStack.getMin();
        minStack.pop();
        minStack.getMin();

    }
}
